package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.artisan.dao.CourseDao;
import com.artisan.dao.TeacherDao;
import com.artisan.model.Course;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;

public class AddCourseFrm extends JInternalFrame {
	private JTextField courseNameTextField;
	private JTextField studentNumTextField;
	private JComboBox teacherListComboBox;
	private JTextArea courseInfoTextArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddCourseFrm frame = new AddCourseFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddCourseFrm() {
		setFrameIcon(new ImageIcon(AddCourseFrm.class.getResource("/com/artisan/image/新人课程.png")));
		setClosable(true);
		setIconifiable(true);

		setTitle("添加课程");
		setBounds(100, 100, 588, 515);

		JLabel lblNewLabel = new JLabel("课程名称:");
		lblNewLabel.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/com/artisan/image/课程列表.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		courseNameTextField = new JTextField();
		courseNameTextField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("授课老师:");
		lblNewLabel_1.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/com/artisan/image/老师.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		teacherListComboBox = new JComboBox();

		JLabel lblNewLabel_2 = new JLabel("学生人数:");
		lblNewLabel_2.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/com/artisan/image/人数.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentNumTextField = new JTextField();
		studentNumTextField.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("课程介绍:");
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblNewLabel_3.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/com/artisan/image/介绍.png")));

		courseInfoTextArea = new JTextArea();

		JButton addCourseButton = new JButton("确认添加");
		addCourseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				addCourseAct(ae);
			}
		});
		addCourseButton.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/com/artisan/image/确认.png")));
		addCourseButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JButton resetButton = new JButton("重置信息");
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				resetValue(ae);
			}
		});
		resetButton.setIcon(new ImageIcon(AddCourseFrm.class.getResource("/com/artisan/image/重置.png")));
		resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(107).addGroup(groupLayout
						.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.UNRELATED).addComponent(courseNameTextField,
										GroupLayout.PREFERRED_SIZE, 286, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup().addGroup(groupLayout
								.createParallelGroup(Alignment.TRAILING)
								.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
												.addComponent(lblNewLabel_2, GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(lblNewLabel_1, GroupLayout.DEFAULT_SIZE, 87,
														Short.MAX_VALUE))
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
												.addComponent(teacherListComboBox, 0, GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(studentNumTextField, GroupLayout.DEFAULT_SIZE, 287,
														Short.MAX_VALUE)))
								.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel_3).addGap(18)
										.addComponent(courseInfoTextArea, GroupLayout.PREFERRED_SIZE, 286,
												GroupLayout.PREFERRED_SIZE)))
								.addPreferredGap(ComponentPlacement.RELATED, 50, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup().addGap(57).addComponent(addCourseButton)
								.addPreferredGap(ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
								.addComponent(resetButton).addGap(16)))
						.addGap(84)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup().addGap(32)
										.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(lblNewLabel).addComponent(courseNameTextField,
														GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(lblNewLabel_1).addComponent(teacherListComboBox,
														GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)))
								.addGroup(groupLayout.createSequentialGroup().addGap(132)
										.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(lblNewLabel_2).addComponent(studentNumTextField,
														GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))))
						.addGap(18)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_3)
								.addComponent(courseInfoTextArea, GroupLayout.PREFERRED_SIZE, 155,
										GroupLayout.PREFERRED_SIZE))
						.addGap(55).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(addCourseButton).addComponent(resetButton))
						.addContainerGap(67, Short.MAX_VALUE)));
		getContentPane().setLayout(groupLayout);
		setTeacherCombox();
	}

	protected void resetValue(ActionEvent ae) {
		// TODO 自动生成的方法存根
		courseNameTextField.setText("");
		courseInfoTextArea.setText("");
		studentNumTextField.setText("");
		teacherListComboBox.setSelectedIndex(0);
	}

	protected void addCourseAct(ActionEvent ae) {
		// TODO 自动生成的方法存根
		String courseName = courseNameTextField.getText().toString();
		String courseInfo = courseInfoTextArea.getText().toString();
		Teacher selectedTeacher = (Teacher) teacherListComboBox.getSelectedItem();
		int studentMaxNum = 0;
		try {
			studentMaxNum = Integer.parseInt(studentNumTextField.getText());
		} catch (Exception e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(this, "学生人数只能输入数字!");
			return;
		}
		if (StringUtil.isEmpty(courseName)) {
			JOptionPane.showMessageDialog(this, "请输入课程名称!");
			return;
		}
		if (studentMaxNum <= 0) {
			JOptionPane.showMessageDialog(this, "学生人数只能输入大于0的数字!");
			return;
		}
		Course course = new Course();
		course.setName(courseName);
		course.setMax_student_num(studentMaxNum);
		course.setInfo(courseInfo);
		course.setTeacher_id(selectedTeacher.getId());
		CourseDao courseDao = new CourseDao();
		if (courseDao.addCourse(course)) {
			JOptionPane.showMessageDialog(this, "添加成功!");
		} else {
			JOptionPane.showMessageDialog(this, "添加失败!");
		}
		courseDao.closeDao();
		resetValue(ae);
	}

	private void setTeacherCombox() {
		// TODO 自动生成的方法存根
		TeacherDao teacherDao = new TeacherDao();
		List<Teacher> teacherList = teacherDao.getTeacherList(new Teacher());
		teacherDao.closeDao();
		for (Teacher teacher : teacherList) {
			teacherListComboBox.addItem(teacher);
		}
	}
}
